import {
  getFundInfo,
  getFundCodePosition,
  getFundOptionPositionTwo,
  getFundOptionPositionFour,
  getDateList,
  getFundBaseValue2,
  getFundBaseValue4,
} from '../../../../services/mail_service';

export default {
  namespace: 'mailManageRule',
  state: {
    fundInfoData: {
      code: '',
      message: '',
      data: [],
    },

    fundCodePositionData: {
      code: '',
      message: '',
      data: [],
    },

    fundOptionPositionTwoData: {
      code: '',
      message: '',
      data: [],
    },

    fundOptionPositionFourData: {
      code: '',
      message: '',
      data: [],
    },

    dateListData: {
      code: '',
      message: '',
      data: [],
    },

    fundBaseValue2Data: {
      code: '',
      message: '',
      data: {
        fundInfo: [],
        fundMonthModels: [],
      },
    },

    fundBaseValue4Data: {
      code: '',
      message: '',
      data: {
        fundInfo: [],
        fundMonthModels: [],
      },
    },
    offlineData: [],
  },

  effects: {
    *GetFundInfo({ payload, callback }, { call, put }) {
      const response = yield call(getFundInfo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundInfoData',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetFundCodePosition({ payload, callback }, { call, put }) {
      const response = yield call(getFundCodePosition, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundCodePosition',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetFundOptionPositionTwo({ payload, callback }, { call, put }) {
      const response = yield call(getFundOptionPositionTwo, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundOptionPositionTwo',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetFundOptionPositionFour({ payload, callback }, { call, put }) {
      const response = yield call(getFundOptionPositionFour, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundOptionPositionFour',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetDateList({ payload, callback }, { call, put }) {
      const response = yield call(getDateList, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveDateListData',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetFundBaseValue2({ payload, callback }, { call, put }) {
      const response = yield call(getFundBaseValue2, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundBaseValue2Data',
        payload: response,
      });
      if (callback) callback(response);
    },

    *GetFundBaseValue4({ payload, callback }, { call, put }) {
      const response = yield call(getFundBaseValue4, payload);
      if (typeof response === 'undefined') {
        return;
      }
      yield put({
        type: 'saveFundBaseValue4Data',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    saveFundInfoData(state, action) {
      return {
        ...state,
        fundInfoData: action.payload,
      };
    },

    saveFundCodePosition(state, action) {
      return {
        ...state,
        fundCodePositionData: action.payload,
      };
    },

    saveFundOptionPositionTwo(state, action) {
      return {
        ...state,
        fundOptionPositionTwoData: action.payload,
      };
    },

    saveFundOptionPositionFour(state, action) {
      return {
        ...state,
        fundOptionPositionFourData: action.payload,
      };
    },

    saveDateListData(state, action) {
      return {
        ...state,
        dateListData: action.payload,
      };
    },

    saveFundBaseValue2Data(state, action) {
      return {
        ...state,
        fundBaseValue2Data: action.payload,
      };
    },

    saveFundBaseValue4Data(state, action) {
      return {
        ...state,
        fundBaseValue4Data: action.payload,
      };
    },
  },
};
